package com.atguigu.test

import java.text.SimpleDateFormat
import scala.io.Source

object Test3Again {
  //用户小时登陆最多多少次
  def main(args: Array[String]): Unit = {
    //读取文件
    val stringlist = Source.fromFile("datas/小时登录次数").getLines().toList
    //1.列裁剪(user,time)
    val list = stringlist.map(line => {
      val arr = line.split(",")
      val user = arr.head
      val time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(arr.last).getTime
      (user, time)
    })
    //2.遍历左边匹配右表 1对多
    list.map {
      case (user,time) =>
      val rlist = list.filter {
        case (ruser, rtime) => user == ruser && rtime - time <= 3600000 && rtime >= time
      }
        (user,rlist.length)
    }
    //3.user分组 user=>list((user,num),(),())
      .groupBy{
        case (user,num)=> user
      }
    //4.map取list再maxby
      .map{
        case (user,list) => list.maxBy(_._2)
      }
    //7.展现
      .foreach(println)
  }
}

